package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootBfDetail;
import org.footballmanager.pandafootball.system.domain.FootBfMvp;
import org.footballmanager.pandafootball.system.domain.FootBfinfo;
import org.footballmanager.pandafootball.system.domain.FootPersonView;
import org.footballmanager.pandafootball.system.form.impl.FootPersonViewSearchForm;
import org.footballmanager.pandafootball.system.service.FootBfDetailService;
import org.footballmanager.pandafootball.system.service.FootBfMvpService;
import org.footballmanager.pandafootball.system.service.FootPersonViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class FootPersonViewServiceImpl extends ServiceImpl<FootPersonView> implements FootPersonViewService {
    @Autowired
    private FootBfDetailService footBfDetailService;
    @Autowired
    private FootBfMvpService footBfMvpService;

    /**
     * 查询
     *
     * @param entityname               实体类名
     * @param map                      参数
     * @param footPersonViewSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootPersonViewSearchForm footPersonViewSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footPersonViewSearchForm);
        Page<FootPersonView> page = PageHelper.startPage(footPersonViewSearchForm.getPageIndex(), footPersonViewSearchForm.getPageSize());
        List<FootPersonView> list = this.findall(footPersonViewSearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footPersonViewSearchForm 条件
     * @param request                  请求
     */
    @Override
    public void deleteAll(FootPersonViewSearchForm footPersonViewSearchForm, HttpServletRequest request) {
        if (footPersonViewSearchForm.getSelectIds() != null) {
            String[] result = footPersonViewSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 根据报分记录更新球员记录
     *
     * @param bfinfo  报分实体类
     * @param request 请求
     */
    @Override
    public void updateData(FootBfinfo bfinfo, HttpServletRequest request) {
        if (bfinfo == null || bfinfo.getSj() == null) {
            return;
        }
        List<FootBfDetail> list = footBfDetailService.findByBfId(bfinfo.getId());
        if (list != null && !list.isEmpty()) {
            for (FootBfDetail detail : list) {
                processByType(detail.getType(), detail.getFupId(), request, bfinfo.getSj());
            }
        }
        FootBfMvp mvp = footBfMvpService.findByBfId(bfinfo.getId());
        processByType(3, mvp.getFupId(), request, bfinfo.getSj());
    }

    private void processByType(Integer type, Long fupId, HttpServletRequest request, Long sj) {
        Example example = new Example(FootPersonView.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("fupId", fupId);
        criteria.andEqualTo("delFlag", 0);
        FootPersonView view = this.findByExample(example);
        if (view != null) {
            setInfo(view, type);
            this.updateNotNull(view, request);
        } else {
            view = new FootPersonView();
            view.setFupId(fupId);
            view.setSj(sj);
            setInfo(view, type);
            this.save(view, request);
        }
    }

    private void setInfo(FootPersonView view, Integer type) {
        if (type == 1) {
            view.setJq(null == view.getJq() ? 1 : view.getJq() + 1);
        } else if (type == 2) {
            view.setZg(null == view.getZg() ? 1 : view.getZg() + 1);
        } else {
            view.setMvp(null == view.getMvp() ? 1 : view.getMvp() + 1);
        }
    }

}
